package com.ttbj.util;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ttbj.product.bean.dto.ViewData;
import com.zywl.common.utils.JsonUtils;
 

public class SafetyUtils {
	private static final Logger LOGGER = LoggerFactory.getLogger(SafetyUtils.class);

	/**
	 * 加盐
	 * @param pwd
	 * @param name
	 * @return
	 */
	public static String addSalt(String pwd, String name) {
		int hashIterations = 1;
		ByteSource salt = ByteSource.Util.bytes(name);// 以账号作为盐值
		SimpleHash hash = new SimpleHash("md5", pwd, salt, hashIterations);
		return hash.toHex();
	}

	/**
	 * 拦截器返回错误信息
	 * @param response
	 * @param model
	 * @param error
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	public static void error(HttpServletResponse response, ViewData model, String error)
			throws UnsupportedEncodingException, IOException {
		LOGGER.info(error);
		response.setContentType("application/json;charset=utf-8");
		OutputStream os = response.getOutputStream();
		os.write(JsonUtils.toLogJson(model).getBytes("UTF-8"));
		os.flush();
		os.close();
	}
	
}
